<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.*,com.tianxiabuyi.persist.*,com.tianxiabuyi.model.*,com.tianxiabuyi.utils.*"%>
<%@ page import="com.alibaba.fastjson.TypeReference" %>
<%!
	static final String url = "http://plat.jssz12320.cn:8001/szregplat/yuyue.wsdl";
	static final String userName = "eeesys";
	static final String password = "appclient.2015";

	// 忽略12320专家姓名中的特殊字符
	public String nameIgnoreSpecial(String name) {
		name = Tools.getDesc(name, "");
		name = name.replace("①", "").replace("\\*", "").replace("(伤)", "").replace("(外)", "").replace("(特)", "").replace("*", "");
		return name;
	}
	void saveDept(String hospitalName, String branch, String branchName, Integer hospital) {
		QBuilder.db("basedb").execHQL("delete from Dept where hospital=" + hospital + " and type=" + DEPT_ORDINARY);
		String action = "GetHospDepartBasic";
		Date startDate = new Date();
		startDate.setDate(startDate.getDate() + 1);
		Date endDate = new Date();
		endDate.setDate(endDate.getDate() + 8);
		String param = String.format("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:GetHospInfoReq xmlns:m=\"http://new.webservice.namespace\"><m:AuthInfo><m:UserName>%s</m:UserName><m:Password>%s</m:Password></m:AuthInfo><m:Filter><m:OperType>%s</m:OperType><m:HospName>%s</m:HospName><m:DepartName>%s</m:DepartName><m:DoctorName>%s</m:DoctorName><m:BeginDate>%s</m:BeginDate><m:EndDate>%s</m:EndDate></m:Filter></m:GetHospInfoReq></SOAP-ENV:Body></SOAP-ENV:Envelope>", new Object[] {userName, password, action, hospitalName, "", "", format(startDate), format(endDate) });
		Map<String, Object>  res=  soap(param);
		List<Map> list = (List<Map>)((Map)((Map)(((Map)(Map)res.get("Body")).get("GetHospInfoRsp"))).get("Hospital")).get("Depart");
		List<Dept> depts = new ArrayList<Dept>();
		for (Map map:list) {
			Dept dept = new Dept();
			dept.setType(DEPT_ORDINARY);
			dept.setDeptName(Tools.getDesc(map.get("DepartName")));
			String deptType = Tools.getDesc(((Map)map.get("DepartBasic")).get("DepartType"));
			if (!deptType.contains("普通")) {
				continue;
			}
			dept.setDeptId(Tools.getMD5(dept.getDeptName()));
			dept.setHospital(hospital);
			dept.setBranch(branch);
			dept.setBranchName(branchName);
			depts.add(dept);
		}
		DBTools.db("basedb").addItem(depts);
	}
	Map<String, Object> soap(String param) {
		String result = null;
		try {
		  result = HttpUtil.soap(url, param);
		} catch (Exception e) {
		  e.printStackTrace();
			return new HashMap();
		}
		if (result == null) return new HashMap();
		return XmlUtil.dom2Map(XmlUtil.parse(result));
	}

	String format(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		return sdf.format(date);
	}

	// 专家列表原始数据
	Map<String, Object> experts(String hospitalName) {
		String action = "GetSchedualList";
		Date startDate = new Date();
		startDate.setDate(startDate.getDate() + 1);
		Date endDate = new Date();
		endDate.setDate(endDate.getDate() + 8);
		String param = String.format("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:GetHospInfoReq xmlns:m=\"http://new.webservice.namespace\"><m:AuthInfo><m:UserName>%s</m:UserName><m:Password>%s</m:Password></m:AuthInfo><m:Filter><m:OperType>%s</m:OperType><m:HospName>%s</m:HospName><m:DepartName>%s</m:DepartName><m:DoctorName>%s</m:DoctorName><m:BeginDate>%s</m:BeginDate><m:EndDate>%s</m:EndDate></m:Filter></m:GetHospInfoReq></SOAP-ENV:Body></SOAP-ENV:Envelope>", new Object[] {userName, password, action, hospitalName, "", "", format(startDate), format(endDate) });
		return soap(param);
	}

	// 生成一条排班表map
	public Schedule scheduldMap(String branch, String branchName, String deptName, String doctorName, Map map, Integer hospital) {
		String ignoreName = nameIgnoreSpecial(doctorName);
		Schedule schedule = new Schedule();
		schedule.setBranch(branch);
		schedule.setBranchName(branchName);
		schedule.setDeptId(Tools.getMD5(deptName, null));
		schedule.setDeptName(deptName);
		schedule.setDoctorId(Tools.getMD5(ignoreName, null));
		schedule.setDoctorName(doctorName);
		schedule.setHospital(hospital);
		schedule.setRegisterName("专家");
		schedule.setRegisterType(2);
		schedule.setDate(map.get("WorkDate") + "");
		if("上午".equals(map.get("WorkType"))) {
			schedule.setHalf(1);
		}else if("下午".equals(map.get("WorkType"))){
			schedule.setHalf(2);
		}
		String workStatus = map.get("WorkStatus") + "";
		if (workStatus.equals("正常")) {
			schedule.setWorkStatus(0);
		} else if (workStatus.equals("停诊")) {
			schedule.setWorkStatus(1);
		} else {
			schedule.setWorkStatus(2);
		}
		schedule.setRegistryFee(Tools.getFloat(map.get("ExpertFee") + ""));
		return schedule;
	}

	// 生成一个预约科室map
	public HashMap deptMap(String deptName) {
		HashMap deptMap = new HashMap();
		deptMap.put("id", deptName);
		deptMap.put("name", deptName);
		return deptMap;
	}

	// 对于没有院区的医院用这个方法
	public void createSchedule(String haopitalName, Integer hospital) {
		createSchedule(haopitalName, hospital, "1", "本部");
	}

	// 添加排班科室和排班医生到数据库
	public void createSchedule(String haopitalName, Integer hospital, String branch, String branchName) {
		Map<String, Object> map = experts(haopitalName);
		List list = new ArrayList();
		try {
			list = (List)(((Map)((Map)(((Map)map.get("Body")).get("GetHospInfoRsp"))).get("Hospital")).get("Depart"));
		} catch (Exception e){}
		// key＝科室id，这里也是科室名称 value＝{"id":"","name":""}
		HashMap<String, HashMap<String, String>> allDeptMap = new HashMap<String, HashMap<String, String>>();
		// 所有排班
		List schedualList = new ArrayList();
		for(int i = 0; i < list.size(); i++) {
			map = (Map)list.get(i);
			if(null == map.get("Doctor")) continue;
			Object obj = map.get("Doctor");
			String key = (String)map.get("DepartName");
			if("java.util.HashMap".equals(obj.getClass().getName())) {
				Map all = (Map)obj;
				String doctorName = (String)all.get("DoctorName");
				if(null == all.get("Schedual")) continue;
				allDeptMap.put(key, deptMap(key));
				Object scheduals = all.get("Schedual");
			    if("java.util.HashMap".equals(scheduals.getClass().getName())) {
			    	Schedule schedule = scheduldMap(branch, branchName, key, doctorName, (Map)scheduals, hospital);
			    	schedualList.add(schedule);
			   	}else if("java.util.ArrayList".equals(scheduals.getClass().getName())) {
					ArrayList allScheduals= (ArrayList)scheduals;
					for(int j = 0; j < allScheduals.size(); j++) {
						Schedule schedule = scheduldMap(branch, branchName, key, doctorName, (Map)(allScheduals.get(j)), hospital);
			    		schedualList.add(schedule);
					}
				}
			} else if("java.util.ArrayList".equals(obj.getClass().getName())) {
				ArrayList scheduals = (ArrayList)obj;
				for(int j = 0; j < scheduals.size(); j++) {
					Map all = (Map)scheduals.get(j);
					String doctorName = (String)all.get("DoctorName");
					if(null == all.get("Schedual")) continue;
					allDeptMap.put(key, deptMap(key));
					Object schedual = all.get("Schedual");
					if("java.util.HashMap".equals(schedual.getClass().getName())) {
						Schedule schedule = scheduldMap(branch, branchName, key, doctorName, (Map)schedual, hospital);
			    		schedualList.add(schedule);
					}else if("java.util.ArrayList".equals(schedual.getClass().getName())) {
						ArrayList allScheduals = (ArrayList)schedual;
						for(int t = 0; t < allScheduals.size(); t++) {
							Schedule schedule = scheduldMap(branch, branchName, key, doctorName, (Map)(allScheduals.get(t)), hospital);
		    				schedualList.add(schedule);
		    			}
					 }
				}
			}
		}

		// 添加到数据库
		List sections = new ArrayList();
		int index = 0;
		for (Entry<String, HashMap<String, String>> entry : allDeptMap.entrySet()) {
			HashMap<String, String> sMap = entry.getValue();
			Dept dept = new Dept();
			dept.setHospital(hospital);
			dept.setType(6);
			dept.setHospitalOrder(++index);
			dept.setDeptName(sMap.get("name"));
			dept.setDeptId(Tools.getMD5(sMap.get("id"), null));
			dept.setBranch(branch);
			dept.setBranchName(branchName);
			sections.add(dept);
		}
		if (sections.size() > 0) {
			QBuilder.db("basedb").execHQL("delete from Dept where type=6 and hospital="+hospital +" and branch="+branch);
			DBTools.db("basedb").addItem(sections);
		}

		if (schedualList.size() > 0) {
			QBuilder.db("basedb").execHQL("delete from Schedule where hospital="+hospital +" and branch="+branch);
			DBTools.db("basedb").addItem(schedualList);
		}
	}

	// 查询排班时间段
	public String schedualTime(String hospitalName, String deptName, String doctorName, String date, String half) {
		String halfString = "上午";
		if ("2".equals(half)) {
			halfString = "下午";
		}
		String param = String.format("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:GetRegPoolReq xmlns:m=\"http://new.webservice.namespace\"><m:AuthInfo><m:UserName>%s</m:UserName><m:Password>%s</m:Password></m:AuthInfo><m:Filter><m:HospName>%s</m:HospName><m:DepartName>%s</m:DepartName><m:DoctorName>%s</m:DoctorName><m:WorkDate>%s</m:WorkDate><m:WorkType>%s</m:WorkType></m:Filter></m:GetRegPoolReq></SOAP-ENV:Body></SOAP-ENV:Envelope>", new Object[] {userName, password, hospitalName, deptName, doctorName, date, halfString});
		try {
			Map map = soap(param);
			String json = JSONUtil.toJson(((Map)((Map)map).get("Body")).get("GetRegPoolRsp"));
			json = json.replaceAll("Result","result").replaceAll("ResultMsg","resultMsg").replaceAll("ResultCode","resultCode").replaceAll("PoolInfo","poolInfo").replaceAll("LeftNum","left_num").replaceAll("FinishedNum","finished_num").replaceAll("TotalNum","total_num").replaceAll("EndTime","end_time").replaceAll("StartTime","start_time");
			Map<String, Object> obj = JSONUtil.fromJson(json, new TypeReference<Map<String, Object>>(){});
			Map resultMap = (Map)obj.get("result");
			String code = Tools.getDesc(resultMap.get("resultCode"), "-1");
			Message msg = new Message(0);
			if ("0".equals(code)) {
				if (obj.get("poolInfo") instanceof List) {
					msg.put("data", obj.get("poolInfo"));
				} else if (obj.get("poolInfo") instanceof Map) {
					List list = new ArrayList();
					list.add(obj.get("poolInfo"));
					msg.put("data", list);
				}
				return msg.toJson();
			}
			msg.setErrcode(Tools.getInt(code));
			msg.setErrmsg(Tools.getDesc(resultMap.get("resultMsg"), ""));
			msg.put("data", new ArrayList());
			return msg.toJson();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Message(30041, "未获取到分时排班").toJson();
	}

	// 预约
	public String register(Integer uid, Map<String, String> parameters, String hospitalName) {
		String userprofileId = parameters.get("pid");
		List<UserProfile> list = QBuilder.db("basedb").queryHql("from UserProfile where uid =" + uid + " and id=" + userprofileId);
		if (list.size() == 0) {
			Message msg = new Message(50000, "预约失败，该用户不是你的常用就诊人");
			return msg.toJson();
		}
		UserProfile userprofile = list.get(0);
		parameters.put("hospitalName", hospitalName);
		parameters.put("cardNumber", userprofile.getCardNumber());
		parameters.put("name", userprofile.getName());
		parameters.put("medicalType", userprofile.getMedicalType());
		parameters.put("phone", userprofile.getPhone());
		Map<String, String> obj = register(parameters);
		String code = obj.get("ResultCode");
		String desc = obj.get("ResultMsg");

		if ("数据与保存的不匹配".equals(desc)) { // 12320与本地数据库中不一致
			// 修改12320信息
			modInfo(userprofile.getCardNumber(), userprofile.getPhone(), userprofile.getName(), userprofile.getMedicalType());
			obj = register(parameters);
			code = obj.get("ResultCode");
			desc = obj.get("ResultMsg");
		}
		/**
		 * 预约返回数据
		 * {Body={RegisterRsp={Result={ResultMsg=成功, ResultCode=0}, ClinicSerialNo=6, FetchTime=2016-06-20 07:30-08:00, SN=5681240}}, Header=}
		 */
		if ("0".equals(code)) { // 预约成功
			Integer hospital = Tools.getInt(parameters.get("hospital")+"");
			String patientName = Tools.getDesc((String)parameters.get("name"), "");
			String deptId = Tools.getDesc((String)parameters.get("dept_id"), "");
			String deptName = Tools.getDesc((String)parameters.get("dept_name"), "");
			String doctorId = Tools.getDesc((String)parameters.get("expert_id"), "");
			String doctorName = Tools.getDesc((String)parameters.get("expert_name"), "");
			String date = Tools.getDesc((String)parameters.get("date"), "");
			String half = Tools.getDesc((String)parameters.get("half"), "");
			RegisterOrder registerOrder = new RegisterOrder();
			registerOrder.setUserProfileId(Tools.getInt(userprofileId));
			registerOrder.setPatientName(patientName);
			registerOrder.setDeptId(deptId);
			registerOrder.setDeptName(deptName);
			registerOrder.setDoctorId(doctorId);
			registerOrder.setDoctorName(doctorName);
			registerOrder.setDate(date);
			registerOrder.setHalf(half);
			registerOrder.setHospital(hospital);
			String sn = obj.get("sn");
			registerOrder.setOrderId(sn);
			registerOrder.setRegisterStatus(1);
			DBTools.db("basedb").addItem(registerOrder);
		}
		desc = desc.replaceAll("申请预约的时间段不满足预约规则。", "每天13:00前不可预约第七天专家号！");
		Message msg = new Message(Tools.getInt(code, 50000), desc);
		return msg.toJson();
	}

	// 取消预约
	public String cancel(Integer uid, Map<String, String> parameters, String hospitalName) {
		String name = parameters.get("name");
		List<UserProfile> list = QBuilder.db("basedb").queryHql("from UserProfile where uid =" + uid + " and name='" + name + "'");
		if (list.size() == 0) {
			Message msg = new Message(50000, "取消预约失败，该用户不是你的常用就诊人");
			return msg.toJson();
		}
		UserProfile userprofile = list.get(0);
		parameters.put("act", "DelReg");
		parameters.put("hospitalName", hospitalName);
		parameters.put("cardNumber", userprofile.getCardNumber());
		parameters.put("name", userprofile.getName());
		parameters.put("medicalType", userprofile.getMedicalType());
		parameters.put("phone", userprofile.getPhone());
		Map<String, String> obj = register(parameters);
		String code = obj.get("ResultCode");
		String desc = obj.get("ResultMsg");

		if ("数据与保存的不匹配".equals(desc)) { // 12320与本地数据库中不一致
			// 修改12320信息
			modInfo(userprofile.getCardNumber(), userprofile.getPhone(), userprofile.getName(), userprofile.getMedicalType());
			obj = register(parameters);
			code = obj.get("ResultCode");
			desc = obj.get("ResultMsg");
		}
		if ("0".equals(code)) { // 取消成功
			String sn = Tools.getDesc(parameters.get("sn"));
			List<RegisterOrder> orders = QBuilder.db("basedb").queryHql("from RegisterOrder where userProfileId=" + userprofile.getId() + " and hospital = " + userprofile.getHospital() + " and orderId='" + sn + "'");
			if (orders.size() != 0 ) {
				RegisterOrder registerOrder = orders.get(0);
				registerOrder.setRegisterStatus(0);
				registerOrder.setModTime(System.currentTimeMillis());
				DBTools.db("basedb").modItem(registerOrder, registerOrder.getId());
			}
		}
		desc = desc.replaceAll("申请预约的时间段不满足预约规则。", "每天13:00前不可预约第七天专家号！");
		Message msg = new Message(Tools.getInt(code, 50000), desc);
		return msg.toJson();
	}

	// 向12320发送预约或取消预约请求
	Map<String, String> register(Map<String, String> params) {
    String hospital = (String)params.get("hospitalName");
    String action = Tools.getDesc((String)params.get("act"), "AddReg");
    String idCard = Tools.getDesc((String)params.get("cardNumber"), "");
    String SN = Tools.getDesc((String)params.get("sn"), "");
    String name = Tools.getDesc((String)params.get("name"), "");
    String phone = Tools.getDesc((String)params.get("phone"), "");
    String insureType = Tools.getDesc((String)params.get("medicalType"), "");
    String sectionName = Tools.getDesc((String)params.get("dept_name"), "");
    String doctorName = Tools.getDesc((String)params.get("expert_name"), "");
    String workDate = Tools.getDesc((String)params.get("date"), "");
    String half = Tools.getDesc((String)params.get("half"), "");
		String halfString = "上午";
		if ("2".equals(half)) {
			halfString = "下午";
		}
    String startTime = Tools.getDesc((String)params.get("start_time"), "");
    String endTime = Tools.getDesc((String)params.get("end_time"), "");
    String regVia = Tools.getDesc((String)params.get("regVia"), "手机：APP");
    String vendor = Tools.getDesc((String)params.get("vendor"), "");
    String param = String.format("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:RegisterReq xmlns:m=\"http://new.webservice.namespace\"><m:AuthInfo><m:UserName>%s</m:UserName><m:Password>%s</m:Password></m:AuthInfo><m:OperType>%s</m:OperType><m:SN>%s</m:SN><m:PatientInfo><m:IdCard>%s</m:IdCard><m:Name>%s</m:Name><m:Phone>%s</m:Phone><m:InsureType>%s</m:InsureType></m:PatientInfo><m:RegInfo><m:HospName>%s</m:HospName><m:DepartName>%s</m:DepartName><m:DoctorName>%s</m:DoctorName><m:WorkDate>%s</m:WorkDate><m:WorkType>%s</m:WorkType><m:StartTime>%s</m:StartTime><m:EndTime>%s</m:EndTime></m:RegInfo><m:RegFrom><m:RegVia>%s</m:RegVia><m:Vendor>%s</m:Vendor><m:Operator>%s</m:Operator></m:RegFrom></m:RegisterReq></SOAP-ENV:Body></SOAP-ENV:Envelope>", new Object[] {userName, password, action, SN, idCard, name, phone, insureType, hospital, sectionName, doctorName, workDate, halfString, startTime, endTime, regVia, vendor, name });
		Map<String, Object> obj = soap(param);
		Map resultMap = (Map)(((Map)((Map)obj).get("Body")).get("RegisterRsp"));
		Map<String, String> reMap = (Map<String, String>)resultMap.get("Result");
		reMap.put("sn", Tools.getDesc(resultMap.get("SN") + "", ""));
		return reMap;
  }

	Map<String, Object> modInfo(String idCard, String phone, String name, String insureType) {
	  String action = "ModReg";
	  String param = String.format("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:RegisterReq xmlns:m=\"http://new.webservice.namespace\"><m:AuthInfo><m:UserName>%s</m:UserName><m:Password>%s</m:Password></m:AuthInfo><m:OperType>%s</m:OperType><m:SN></m:SN><m:PatientInfo><m:IdCard>%s</m:IdCard><m:Name>%s</m:Name><m:Phone>%s</m:Phone><m:Sex></m:Sex><m:Birth></m:Birth><m:InsureType>%s</m:InsureType></m:PatientInfo><m:RegFrom><m:RegVia>app</m:RegVia><m:Vendor>12320</m:Vendor><m:Operator>%s</m:Operator></m:RegFrom></m:RegisterReq></SOAP-ENV:Body></SOAP-ENV:Envelope>", new Object[] {userName, password, action, idCard, name, phone, insureType,name });
	  return soap(param);
	}

	// 我的预约
	public String regInfo(Integer hospital, String hospitalName, Integer uid, Integer branch) {
		List<UserProfile> list = QBuilder.db("basedb").queryHql("from UserProfile where uid=" + uid + " and status = " + STATUS_ENABLE);
		List regs = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			UserProfile p = list.get(i);

			String idCard = p.getCardNumber();
			String patientName = p.getName();
    	String beginDate = DateUtil.formatDate(DateUtil.afterNDay(new Date(), -90));
    	String endDate = DateUtil.formatDate(DateUtil.afterNDay(new Date(), 10));
    	String name = "";
    	String phone = "";
    	String SN = "";
    	String departName = "";
    	String doctorName = "";

    	String param = String.format("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:GetRegInfoReq xmlns:m=\"http://new.webservice.namespace\"><m:AuthInfo><m:UserName>%s</m:UserName><m:Password>%s</m:Password></m:AuthInfo><m:PatientIdentify><m:SN>%s</m:SN><m:IdCard>%s</m:IdCard><m:Other><m:Name>%s</m:Name><m:Phone>%s</m:Phone></m:Other></m:PatientIdentify><m:Filter><m:BeginDate>%s</m:BeginDate><m:EndDate>%s</m:EndDate><m:HospName>%s</m:HospName><m:DepartName>%s</m:DepartName><m:DoctorName>%s</m:DoctorName></m:Filter></m:GetRegInfoReq></SOAP-ENV:Body></SOAP-ENV:Envelope>", new Object[] {userName, password, SN, idCard, name, phone, beginDate, endDate, hospitalName, departName, doctorName });
			Map map = soap(param);
			map = (Map)((Map)((Map)map).get("Body")).get("GetRegInfoRsp");

			if(map.get("RegInfo") instanceof ArrayList) {
				ArrayList regArray = (ArrayList)map.get("RegInfo");
				for (int j = 0; j < regArray.size(); j++) {
					Map obj = (Map)regArray.get(j);
					obj.put("patient_name", patientName);
				}
				regs.addAll(regArray);
			}	else if(map.get("RegInfo") instanceof Map) {
				Map obj = (Map)map.get("RegInfo");
				obj.put("patient_name", patientName);
				regs.add(obj);
			}
		}

		for (int i = 0; i < regs.size(); i++) {
			Map obj = (Map)regs.get(i);
			obj.remove("RegFrom");
			obj.remove("HospName");
			obj.remove("SelfPay");
		}
		return completeAvatar(regs, hospital, branch);
	}

	// 我的预约
	public String regInfo(Integer hospital, String hospitalName, Integer uid) {
		return regInfo(hospital, hospitalName, uid, 1);
	}

	public String completeAvatar(List list, Integer hospital, Integer branch) {
		// 取出所有医生姓名
		Message msg = new Message();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> orderMap = (Map<String, Object>)list.get(i);
			String name = nameIgnoreSpecial(orderMap.get("DoctorName") + "");
			buffer.append("'" + name + "',");
		}
		if (buffer.length() == 0) {
			msg = new Message(30041, "暂无数据");
			return msg.toJson();
		}
		buffer.deleteCharAt(buffer.length()-1);
		String names = buffer.toString();

		// 根据姓名查询医生头像
		String hql = "from Expert where hospital ='" + hospital +"' and name in(" + names + ") and deptId in (select id from Dept where branch="+branch+")";
		List<Expert> eList = QBuilder.db("basedb").queryHql(hql);
		HashMap<String, String> avatarMap = new HashMap<String, String>();
		for (int i = 0; i < eList.size(); i++) {
			Expert expert = eList.get(i);
			String name = expert.getName();
			String avatar = Tools.getDesc(expert.getAvatar(), "http://image.eeesys.com/default/default_avatar.png");
			avatarMap.put(name, avatar);
		}

		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> orderMap = (Map<String, Object>)list.get(i);
			String name = nameIgnoreSpecial(orderMap.get("DoctorName") + "");
			orderMap.put("avatar", avatarMap.get(name));
		}
		if (list.size() > 0) {
			msg.put("register", list);
		} else {
			msg.setErrcode(30091);
			msg.setErrmsg("暂无预约信息");
		}
		String json = msg.toJson();
		json = json.replaceAll("没有此用户","没有记录").replaceAll("IsQuit","is_quit").replaceAll("QuitTime","quit_time").replaceAll("Remark","remark").replaceAll("IsFetched","is_fetched").replaceAll("InsurePay","insurepay").replaceAll("RegDateTime","reg_date_time").replaceAll("UnpayedFee","unpayed_fee").replaceAll("DepartName","dept_name").replaceAll("DoctorName","expert_name").replaceAll("IsUnPayed","isunpayed").replaceAll("ClinicSN","clinic_SN").replaceAll("ClinicDateTime","clinic_date_time");
		json = json.replaceAll("\\\\\"","\"").replaceAll("\"\\{","\\{").replaceAll("\\}\"","\\}");
		return json;
	}
%>
